package com.wkf.truck.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wkf.truck.common.CommonEnum;
import com.wkf.truck.common.PageModel;
import com.wkf.truck.common.R;
import com.wkf.truck.model.Truck;
import com.wkf.truck.model.User;
import com.wkf.truck.service.TruckService;
import com.wkf.truck.service.UserService;
import com.wkf.truck.vo.TruckTop10Vo;
import com.wkf.truck.vo.TruckVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lxb
 * @since 2021-02-14
 */
@RestController
@RequestMapping("/truck")
public class TruckController {


    @Autowired
    private TruckService truckService;

    @Autowired
    private UserService userService;

    /**
     * 货车搜索
     *
     * @param truckType
     * @return
     */
    @GetMapping("/search")
    public R search(@RequestParam("truckType") String truckType) {

        QueryWrapper<Truck> wrapper = new QueryWrapper<>();
        wrapper.like("type", truckType);
        List<Truck> list = truckService.list(wrapper);

        return R.ok().put("data", list);
    }

    /**
     * 获取货车详情
     *
     * @param truckId
     * @return
     */
    @GetMapping("/getTruckMes")
    public R getInfo(@RequestParam("truckId") Long truckId) {
        TruckVo truckVo = truckService.getInfo(truckId);
        return R.ok().put("data", truckVo);
    }

    /**
     * 货车排名
     *
     * @return
     */
    @GetMapping("/getTruckRank")
    public R getTop10() {

        List<TruckTop10Vo> list = truckService.getTop10();
        return R.ok().put("data", list);
    }

    /**
     * 商家的货车入驻
     *
     * @return
     */
    @PostMapping("/register")
    public R save(@RequestBody Truck truck) {

        if (truck.getType().equals("小面包")) {
            truck.setAdvice(CommonEnum.TruckAdvice.小面包.getAdvide());
            truck.setCapacity(CommonEnum.TruckCapacity.小面包.getCapacity());
            truck.setSpace(CommonEnum.TruckSpace.小面包.getSpace());
        } else if (truck.getType().equals("中面包")) {
            truck.setAdvice(CommonEnum.TruckAdvice.中面包.getAdvide());
            truck.setCapacity(CommonEnum.TruckCapacity.中面包.getCapacity());
            truck.setSpace(CommonEnum.TruckSpace.中面包.getSpace());
        } else if (truck.getType().equals("小货车")) {
            truck.setAdvice(CommonEnum.TruckAdvice.小货车.getAdvide());
            truck.setCapacity(CommonEnum.TruckCapacity.小货车.getCapacity());
            truck.setSpace(CommonEnum.TruckSpace.小货车.getSpace());
        } else if (truck.getType().equals("中箱货")) {
            truck.setAdvice(CommonEnum.TruckAdvice.中箱货.getAdvide());
            truck.setCapacity(CommonEnum.TruckCapacity.中箱货.getCapacity());
            truck.setSpace(CommonEnum.TruckSpace.中箱货.getSpace());
        }
        else if (truck.getType().equals("中平板")) {
            truck.setAdvice(CommonEnum.TruckAdvice.中平板.getAdvide());
            truck.setCapacity(CommonEnum.TruckCapacity.中平板.getCapacity());
            truck.setSpace(CommonEnum.TruckSpace.中平板.getSpace());
        }
        else if (truck.getType().equals("五米二")) {
            truck.setAdvice(CommonEnum.TruckAdvice.五米二.getAdvide());
            truck.setCapacity(CommonEnum.TruckCapacity.五米二.getCapacity());
            truck.setSpace(CommonEnum.TruckSpace.五米二.getSpace());
        }
        else if (truck.getType().equals("六米八")) {
            truck.setAdvice(CommonEnum.TruckAdvice.六米八.getAdvide());
            truck.setCapacity(CommonEnum.TruckCapacity.六米八.getCapacity());
            truck.setSpace(CommonEnum.TruckSpace.六米八.getSpace());
        }
        //保存
        truckService.save(truck);

        //货车保存到用户表
        User user = userService.getOne(new QueryWrapper<User>().eq("id", truck.getUserId()));
        if (user != null) {
            user.setTruckId(truck.getId());
            userService.saveOrUpdate(user);
        }
        return R.ok().put("truckId", truck.getId());
    }

    /**
     * 商家用户信息查看
     *
     * @param userId
     * @return
     */
    @GetMapping("/getMes")
    public R getMes(@RequestParam("userId") Long userId) {

        User user = userService.getOne(new QueryWrapper<User>().eq("id", userId));
        return R.ok().put("data", user);

    }


    /**
     * 获取所有商家列表
     * @param truck
     * @return
     */
    @PostMapping("/getTruckUsers")
    public  R getTruckUsers(@RequestBody Truck truck){
        Page page = truckService.getTruckUsers(truck);
        return R.ok(page);
    }


    /**
     * 刪除
     * @param truckId
     * @return
     */
    @GetMapping("/delete")
    public R delete(@RequestParam("truckId") Long truckId){

        Truck truck = truckService.getById(truckId);

        if(truck == null){
            return R.error("不存在该货车");
        }

        User user = userService.getById(truck.getUserId());

        if(user == null){
            return R.error("不存在该用户");
        }
        //同时删除用户
        truckService.removeById(truckId);
        userService.removeById(user.getId());

        return R.ok();
    }
}
